package com.yzk.aigy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzk.aigy.model.system.SysLoginLog;
import com.yzk.aigy.model.vo.SysLoginLogQueryVo;
import com.yzk.aigy.service.SysLoginLogService;
import com.yzk.aigy.mapper.SysLoginLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @description 针对表【sys_login_log(系统访问记录)】的数据库操作Service实现
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog>
        implements SysLoginLogService {

    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUsername(username);
        sysLoginLog.setStatus(status);
        sysLoginLog.setIpaddr(ipaddr);
        sysLoginLog.setMsg(message);
        sysLoginLog.setAccessTime(new Date());
        baseMapper.insert(sysLoginLog);
    }

    @Override
    public IPage<SysLoginLog> selectPage(SysLoginLogQueryVo sysLoginLogQueryVo) {
        //创建page对象
        Page<SysLoginLog> pageParam = new Page<>(sysLoginLogQueryVo.getPageNum(), sysLoginLogQueryVo.getPageSize());
        //获取条件值
        String username = sysLoginLogQueryVo.getUsername();
        String createTimeBegin = sysLoginLogQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysLoginLogQueryVo.getCreateTimeEnd();
        //封装条件
        QueryWrapper<SysLoginLog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wrapper.like("username", username);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge("create_time", createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.le("create_time", createTimeEnd);
        }
        wrapper.orderByDesc("id");
        //调用mapper方法
        IPage<SysLoginLog> pageModel = baseMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

}




